package com.huaxin.hxcloud.find;


import com.huaxin.common.Entity.FindSearchBean;
import com.huaxin.common.tools.RequestHelper;
import com.huaxin.common.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by hebing on 2017/5/11.
 */

public class SearchModel {

    public IsearchModel isearchModel;

    public SearchModel(IsearchModel isearchModel) {
        this.isearchModel = isearchModel;
    }

    //
    public void requestDatas(int type, String value) {

        final List<FindSearchResultBean> resultBeanList = new ArrayList<>();

        Call<FindSearchBean> call = RequestHelper.getRequestApi().commitSearch(type, value);
        call.enqueue(new Callback<FindSearchBean>() {
            @Override
            public void onResponse(Call<FindSearchBean> call, Response<FindSearchBean> response) {
                if (response.isSuccessful()) {
                    FindSearchBean body = response.body();
                    List<FindSearchBean.InformationArrayBean> informationArray = body.getInformationArray();
                    List<FindSearchBean.UserArrayBean> userArray = body.getUserArray();
                    List<FindSearchBean.QuestionArrayBean> questionArray = body.getQuestionArray();
                    //
                    int count=userArray.size();
                    if (count!= 0) {
                            FindSearchResultBean findSearchResultBean0= new FindSearchResultBean(0, "人物");
                            resultBeanList.add(findSearchResultBean0);
                        for (int i = 0; i <count; i++) {
                            FindSearchResultBean findSearchResultBean= new FindSearchResultBean(1, userArray.get(i));
                            resultBeanList.add(findSearchResultBean);
                        }
                    }
                    count = informationArray.size();
                    if (count!= 0) {
                        FindSearchResultBean findSearchResultBean0= new FindSearchResultBean(0, "资讯");
                        resultBeanList.add(findSearchResultBean0);
                        for (int i = 0; i <count; i++) {
                            FindSearchResultBean findSearchResultBean= new FindSearchResultBean(2, informationArray.get(i));
                            resultBeanList.add(findSearchResultBean);
                        }
                    }
                    count = questionArray.size();
                    if (count!= 0) {
                        FindSearchResultBean findSearchResultBean0= new FindSearchResultBean(0, "问题");
                        resultBeanList.add(findSearchResultBean0);
                        for (int i = 0; i <count; i++) {
                            FindSearchResultBean findSearchResultBean= new FindSearchResultBean(3, questionArray.get(i));
                            resultBeanList.add(findSearchResultBean);
                        }
                    }
                    //数据返回回调方法
                    isearchModel.onDataLoadFinished(resultBeanList);
                }
            }

            @Override
            public void onFailure(Call<FindSearchBean> call, Throwable t) {
                ToastUtil.showToastShort("网络异常");
            }
        });

    }
}
